#!/bin/sh
# Start/stop/restart open-vm-tools.

openvmtools_check() {
  vmware-checkvm
  if [ $? -ne 0 ]; then
    echo "eRRoR with vmware-checkvm"
    exit 1
  fi
}

openvmtools_start() {
  openvmtools_check
  CMDLINE="/usr/bin/vmtoolsd --background=/var/run/vmtoolsd.pid"
  echo -n "Starting vmtoolsd: $CMDLINE"
  $CMDLINE || exit 2
  echo
}

openvmtools_stop() {
  VMPID=$(cat /var/run/vmtoolsd.pid)
  echo -n "Stopping vmtoolsd (PID $VMPID)..."
  kill -9 $VMPID
  echo
}

openvmtools_restart() {
  openvmtools_stop
  sleep 1
  openvmtools_start
}

# Check if openvmtools is running
openvmtools_status() {
  if [ -e /var/run/vmtoolsd.pid ]; then
    echo "vmtoolsd is running."
  else
    echo "vmtoolsd is stopped."
    exit 1
  fi
}

case "$1" in
'start')
  openvmtools_start
  ;;
'stop')
  openvmtools_stop
  ;;
'restart')
  openvmtools_restart
  ;;
'status')
  openvmtools_status
  ;;
*)
  echo "usage $0 start|stop|restart|status"
esac
